#!/bin/bash


APP=edu

if [ -n "$2" ] ;then
   do_date=$2
else 
   echo "请传入日期参数"
   exit
fi 

dws_traffic_page_visitor_page_view_nd="
  insert overwrite table $APP.dws_traffic_page_visitor_page_view_nd partition(dt='$do_date')
select
    mid_id,
    brand,
    model,
    operate_system,
    page_id,
    sum(if(dt>=date_add('$do_date',-6),during_time_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),view_count_1d,0)),
    sum(during_time_1d),
    sum(view_count_1d)
from $APP.dws_traffic_page_visitor_page_view_1d
where dt>=date_add('$do_date',-29)
and dt<='$do_date'
group by mid_id,brand,model,operate_system,page_id;
"

dws_trade_user_cart_add_nd="

insert overwrite table $APP.dws_trade_user_cart_add_nd partition(dt='$do_date')
select
    user_id,
    sum(if(dt>=date_add('$do_date',-6),cart_add_count_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),cart_add_num_1d,0)),
    sum(cart_add_count_1d),
    sum(cart_add_num_1d)
from $APP.dws_trade_user_cart_add_1d
where dt>=date_add('$do_date',-29)
and dt<='$do_date'
group by user_id;


"

dws_trade_user_order_nd="


insert overwrite table $APP.dws_trade_user_order_nd partition(dt='$do_date')
select
    user_id,
    sum(if(dt>=date_add('$do_date',-6),order_count_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),order_num_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),order_original_amount_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),coupon_reduce_amount_1d,0)),
    sum(if(dt>=date_add('$do_date',-6),order_total_amount_1d,0)),
    sum(order_count_1d),
    sum(order_num_1d),
    sum(order_original_amount_1d),
    sum(coupon_reduce_amount_1d),
    sum(order_total_amount_1d)
from $APP.dws_trade_user_order_1d
where dt>=date_add('$do_date',-29)
and dt<='$do_date'
group by user_id;

select sum(order_count_1d)
from $APP.dws_trade_user_order_1d

where dt>=date_add('$do_date',-29)
and dt<='$do_date'
group by user_id;

"

dws_trade_user_payment_nd="

insert overwrite table $APP.dws_trade_user_payment_nd partition (dt = '$do_date')
select user_id,
       sum(if(dt >= date_add('$do_date', -6), payment_count_1d, 0)),
       sum(if(dt >= date_add('$do_date', -6), payment_num_1d, 0)),
       sum(if(dt >= date_add('$do_date', -6), payment_amount_1d, 0)),
       sum(payment_count_1d),
       sum(payment_num_1d),
       sum(payment_amount_1d)
from $APP.dws_trade_user_payment_1d
where dt >= date_add('$do_date', -29)
  and dt <= '$do_date'
group by user_id;

"



case $1 in

"dws_traffic_page_visitor_page_view_nd")
hive -e "$dws_traffic_page_visitor_page_view_nd"
;;
"dws_trade_user_cart_add_nd")
hive -e "$dws_trade_user_cart_add_nd"
;;
"dws_trade_user_order_nd")
hive -e "$dws_trade_user_order_nd"
;;
"dws_trade_user_payment_nd")
hive -e "$dws_trade_user_payment_nd"
;;

"all")

  hive -e "$dws_traffic_page_visitor_page_view_nd$dws_trade_user_cart_add_nd$dws_trade_user_order_nd$dws_trade_user_payment_nd"
  ;;
  esac
